package com.pearadmin.pro.modules.school.service.impl;

import java.util.List;
import java.util.ArrayList;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.school.domain.*;
import com.pearadmin.pro.modules.school.dto.CourseCodeDTO;
import com.pearadmin.pro.modules.school.dto.SchoolDTO;
import com.pearadmin.pro.modules.school.param.CourseCodeRequest;
import com.pearadmin.pro.modules.school.repository.CourseCodeRepository;
import com.pearadmin.pro.modules.school.service.CourseCodeService;
import com.pearadmin.pro.modules.school.service.PreCurriculumRelationshipService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 课程代码Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class CourseCodeServiceImpl extends ServiceImpl<CourseCodeRepository, CourseCode> implements CourseCodeService
{
    @Resource
    private CourseCodeRepository courseCodeRepository;
    @Resource
    private PreCurriculumRelationshipService preCurriculumRelationshipService;

    /**
     * 查询课程代码
     * @param request
     * @return 课程代码 分页集合
     * */
    @Override
    public PageResponse<CourseCode> page(CourseCodeRequest request)
    {
        LambdaQueryWrapper<CourseCode> query = new LambdaQueryWrapper<CourseCode>();
        query.eq(ObjectUtil.isNotEmpty(request.getCourseCode()),CourseCode::getCourseCode,request.getCourseCode());
        query.eq(ObjectUtil.isNotEmpty(request.getCourseName()),CourseCode::getCourseName,request.getCourseName());
        return Pageable.of(request,(()->  courseCodeRepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(CourseCodeDTO courseCode) {

        CourseCode courseCodedb = GeneralConvertUtils.conv(courseCode, CourseCode.class);
        saveOrUpdate(courseCodedb);
        if (!ObjectUtils.isEmpty(courseCode.getPreCurriculumRelationshipList())) {
            List<PreCurriculumRelationship> schoolSendList = GeneralConvertUtils.convert2List(courseCode.getPreCurriculumRelationshipList(), PreCurriculumRelationship.class);
            schoolSendList.stream().forEach(s -> {
                s.setCourseCodeId(courseCodedb.getId());
            });
            preCurriculumRelationshipService.saveOrUpdateBatch(schoolSendList);
        }
        return null;
    }

    /**
     * 查询课程代码
     *
     * @param id 课程代码ID
     * @return 课程代码
     */
    @Override
    public CourseCodeDTO selectCourseCodeById(Long id)
    {
        CourseCodeDTO request = GeneralConvertUtils.conv(courseCodeRepository.selectById(id), CourseCodeDTO.class);

        LambdaQueryWrapper<PreCurriculumRelationship> query = new LambdaQueryWrapper<PreCurriculumRelationship>();
        query.eq(PreCurriculumRelationship::getCourseCodeId, id);
        request.setPreCurriculumRelationshipList(preCurriculumRelationshipService.list(query));
        return request;
    }

}
